<template>
  <div class="dialog">
    <el-dialog :title="'条码类型' + titleText" :visible.sync="showDialog" width="40%" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="条码名称" prop="noType">
          <el-input v-model="formData.noType" placeholder="请输入条码名称" clearable />
        </el-form-item>
        <el-form-item label="别名" prop="noTypeName">
          <el-input v-model="formData.noTypeName" placeholder="请输入条码别名" clearable />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="formData.remarks" placeholder="请输入备注" type="textarea" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import NoTypeApi from "@/api/basic-info-manage/no-type";
import EnumApi from "@/api/enum"
export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    let checkInt = (rule, value, callback) => {
      if (value) {
        if (Number(value) && value % 1 === 0) {
          callback();
        } else {
          return callback(new Error("请输入整数！"));
        }
      }
    };
    return {
      formData: {},
      featuresTypeList: [],
      rules: {
        noType: [
          { required: true, message: "请输入条码名称", trigger: ["change", "blur"] }
        ],
      },
      noticeType: [],
      noticeEvent: [],
      staffList: [],
      showDialog: this.show
    };
  },
  computed: {},
  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    }
  },
  created() {
  },
  mounted() {
    this.queryEnum()
  },
  methods: {
    queryEnum() {
      EnumApi.list({ enumType: 'NoticeType' }).then(res => {
        this.noticeType = res.data
      })
      EnumApi.list({ enumType: 'NoticeEvent' }).then(res => {
        this.noticeEvent = res.data
      })
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            NoTypeApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            NoTypeApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
